<template>
<view class="income-info">
    <view class="c-row-box">
        <view class="c-row-head">
            <view class="c-row-title">收款信息</view>
        </view>
        <view class="c-row-item">
            <view class="c-row c-row-list">
                <view class="c-row-col c-row-col-list-item">
                    <view class="col-title">
                        <text style="color: red;">*</text>
                        收据编号
                    </view>
                    <view class="col-text"><input @blur="checkIncome" type="text" v-model="incomeParams.receivable_no" :disabled="isPreview" placeholder="请输入" /></view>
                </view>
            </view>

            <view class="c-row c-row-list">
                <view class="c-row-col c-row-col-list-item">
                    <view class="col-title">
                        <text style="color: red;">*</text>
                        收款方式
                    </view>
                    <view class="col-text">
                        <picker mode="selector" :range="paymentList" range-key="title" @change="incomePaymentChange" :disabled="isPreview">
                            <view>{{ selectPayment }}</view>
                        </picker>
                    </view>
                </view>
            </view>

            <view class="c-row c-row-list">
                <view class="c-row-col c-row-col-list-item">
                    <view class="col-title">
                        <text style="color: red;">*</text>
                        收款性质
                    </view>
                        <view class="col-text" v-if="type=='fn'">
							<view>{{ selectedIncomeType }}</view>
						</view>
						<view class="col-text" v-else>
							<picker mode="selector" :range="incomeOrderList" range-key="title" @change="incomeOrderChange" :disabled="isPreview">
								<view>{{ selectedIncomeType }}</view>
							</picker>
						</view>
                </view>
            </view>

            <view class="c-row c-row-list" v-if="orderParams.order_nature != 3">
                <view class="c-row-col c-row-col-list-item">
                    <view class="col-title">
                        <text style="color: red;">*</text>
                        收款金额
                    </view>
                    <view class="col-text">
                        <input type="digit" @blur="checkIncome" v-model="incomeParams.banquet_income_item_price" @input="getTailMoney" :disabled="isPreview" placeholder="请输入" />
                    </view>
                </view>
            </view>

            <view class="c-row c-row-list" v-else>
                <view class="c-row-col c-row-col-list-item">
                    <view class="col-title">
                        <text style="color: red;">*</text>
                        收款金额
                    </view>
                    <view class="col-text"><input @blur="checkIncome" type="digit" v-model="incomeParams.banquet_income_item_price" :disabled="isPreview" placeholder="请输入" /></view>
                </view>
            </view>

            <view class="c-row c-row-list">
                <view class="c-row-col c-row-col-list-item">
                    <view class="col-title">
                        <text style="color: red;">*</text>
                        收款时间
                    </view>
                    <view class="col-text">
                        <picker mode="date" :start="startDate" :end="endDate" @change="incomeDateChange" :disabled="isPreview">
                            <view>{{ incomeParams.banquet_income_date }}</view>
                        </picker>
                    </view>
                </view>
            </view>
        </view>
    </view>
</view>
</template>

<script>
import dictionary from '@/utils/dictionary.js';
import request from '@/utils/request.js';
export default {
    props: {
        isPreview: {
            type: Boolean,
            default () {
                return false;
            }
        },
        incomeParams: {
            type: Object,
            default () {
                return {};
            }
        },
        orderParams: {
            type: Object,
            default () {
                return {};
            }
        },
        incomeNature: {
            type: Number,
            default () {
                return null;
            }
        },
        incomeType: {
            type: String,
            default () {
                return '';
            }
        },
        startDate: {
            type: String,
            default () {
                return '';
            }
        },
        endDate: {
            type: String,
            default () {
                return '';
            }
        }
    },
    data() {
        return {
            incomeOrder: [{
                    id: 1,
                    title: '定金'
                },
                {
                    id: 2,
                    title: '全款'
                }
            ],
            incomeOrderList: [{
                    id: 1,
                    title: '定金'
                },
                {
                    id: 2,
                    title: '全款'
                }
            ],
            type:""
        };
    },
    computed: {
        exchangeTypeList: function () {
            let exchangeTypeList = dictionary.getPicker("exchangeTypeList");
            return exchangeTypeList;
        },
        // 支付方式
        paymentList: function () {
            let paymentList = dictionary.getPicker("paymentList");
            return paymentList;
        },
        selectPayment: function () {
            return dictionary.getPickerTitle('paymentList', this.incomeParams.banquet_income_payment, '请选择');
        },
        selectedIncomeType: function () {
            return dictionary.getPickerTitle('exchangeTypeList', this.incomeParams.banquet_income_type, '请选择');
        }
    },
    mounted() {
        this.incomeOrderList = uni.getStorageSync('allPicker').exchangeTypeList;
    },
    methods: {
        		//验证收款性质
		getIncomeNature(type,value){
            let _this =this;
			//创建订单
			if(type=='order'){
				_this.incomeOrderList = _this.incomeOrder
			}
			// 收款
			if(type=='income'){
				_this.incomeOrderList = _this.incomeOrderList
			}
			//自定义
			if(type=='fn'){
                _this.type = type;
                _this.incomeParams.banquet_income_type =value[0].id
				_this.incomeOrderList = value
			}
		},
        // 检查收款编号
        verificationCode(collection_code) {
            if (collection_code != '') {
                request.request({
                    url: '/api/order.collection_code/unique',
                    showLoading: true,
                    token: true,
                    data: {
                        collection_code: collection_code
                    },
                    success(res) {
                        if (res.code == 200) {
                            uni.showToast({
                                icon: 'none',
                                title: res.msg,
                                duration: 3000
                            });
                        } else {
                            uni.showToast({
                                icon: 'none',
                                title: res.msg,
                                duration: 3000
                            });
                        }
                    }
                });
            }
        },
        checkIncome(){
            let _this =this;
        	let incomePrice = _this.incomeParams.banquet_income_item_price;
            if(incomePrice>0){
                _this.verificationCode(_this.incomeParams.receivable_no)
            }
        },
        // 收款日期
        incomeDateChange(e) {
            this.incomeParams.banquet_income_date = e.target.value;
        },

        // 收款方式
        incomePaymentChange(e) {
            console.log(e);
            let index = e.target.value;
            this.incomeParams.banquet_income_payment = this.paymentList[index].id;
        },

        // 当是订单时
        incomeOrderChange(e) {
            let index = e.target.value;
            this.incomeParams.banquet_income_type = this.incomeOrderList[index].id;
        },

        // 自动计算尾款
        getTailMoney(e) {
            this.orderParams.earnest_money = e.detail.value;
            this.$emit('getTailMoney');
        }
    }
};
</script>

<style lang="less">
@import url('../../components/common.less');
@import url('../common/common.less');
</style>
